package com.gxa.p2p.service.impl;


import com.gxa.p2p.mapper.InvestRecordMapper;
import com.gxa.p2p.mapper.LoanBidsDetailMapper;
import com.gxa.p2p.pojo.InvestRecord;
import com.gxa.p2p.pojo.LoanBidsDetail;
import com.gxa.p2p.service.InvestRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class InvestRecordServiceImpl implements InvestRecordService {

    @Resource
    InvestRecordMapper investRecordMapper;

    @Resource
    LoanBidsDetailMapper loanBidsDetailMapper;

    @Override
    public List<Object> findInvest(Integer userId,Integer page,Integer limit) {
        // 统计数量
        // 分页查询;
        List<InvestRecord> investRecords = investRecordMapper.selectByUserId(userId,(page-1)*limit,limit);
        investRecords.get(0).setCount(countInfo(userId));
        // 返回一个list集合
        ArrayList<Object> list = new ArrayList<>();
        list.add(countInfo(userId));
        list.add(investRecords);
        return list;
    }

    @Override
    public List<Object> findInvestDim(Integer userId,String IcoModel, Integer page, Integer limit) {
        List<InvestRecord> investRecords = investRecordMapper.selectByUserIdDim(userId, "%"+IcoModel+"%", page, limit);
        InvestRecord userCountDim = investRecordMapper.findUserCountDim(userId, "%"+IcoModel+"%");
        Integer count = userCountDim.getCount();
        ArrayList<Object> list = new ArrayList<>();
        list.add(count);
        list.add(investRecords);
        return list;
    }

    @Override
    public ArrayList<Object> findUserRec(Integer userId, Integer page, Integer limit) {
        List<LoanBidsDetail> loanBidsDetails = loanBidsDetailMapper.selectByVer(userId,(page-1)*limit,limit);
        // 返回一个list集合
        ArrayList<Object> list = new ArrayList<>();
        list.add(countInfo(userId));
        list.add(loanBidsDetails);
        return list;

    }

    @Override
    public ArrayList<Object> findUserRecDim(Integer userId, String str, Integer page, Integer limit) {
        List<LoanBidsDetail> loanBidsDetails = loanBidsDetailMapper.selectByVerDim(userId,"%"+str+"%",(page-1)*limit,limit);
        // 返回一个list集合
        ArrayList<Object> list = new ArrayList<>();
        list.add(loanBidsDetailMapper.selectByVerDimCount(userId,"%"+str+"%").getCount());
        list.add(loanBidsDetails);
        return list;

    }

    /**
     * 查询某个用户的所有以投标的数量
     * @param userId
     * @return
     */
    private Integer countInfo(Integer userId) {
        return investRecordMapper.findUserCount(userId).getCount();
    }
}
